const router = require('koa-router')()
const Message = require("../models/message");


router.get('/', async function (ctx, next) {
    ctx.body = await Message.find().sort({'createdAt':-1})
})

router.post('/', async function (ctx, next) {
    ctx.verifyParams({
        name: {type: "string", required: true},
        text: {type: "string", required: true}
    })
    ctx.body = await Message.create(ctx.request.body)
})

router.delete('/:id', async function (ctx, next) {
    console.log(11,ctx.params.id)
    let msg = await Message.findById(ctx.params.id)
    if(msg.name == ctx.request.body.name){
        ctx.body = await Message.findByIdAndDelete(ctx.params.id)
    }else {
        ctx.throw(404, '不能删除');
    }
})


module.exports = router
